package client.comm.baoding.utils;


import android.content.Context;
import android.content.SharedPreferences;

import java.util.ArrayList;

import client.comm.baoding.MyApp;
import client.comm.commlib.App;


public class PreferencesManager {
	private static PreferencesManager instance_ ;

	private Context context_;

	private final String PREFERENCE_NAME = "nj_pre";

	public PreferencesManager(Context context) {
		this.context_ = context;
	}

	public static PreferencesManager getInstance() {
		if (instance_ == null){
			instance_ = new PreferencesManager(
					MyApp.Companion.getInstance());
		}
		return instance_;
	}

	public void clearData(String s) {
		context_.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
				.edit().remove(s).commit();

	}
	public boolean putInt(String key, int value) {
		boolean flag = context_
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
				.edit().putInt(key, value).commit();
		return flag;
	}

	public int getInt(String key, int defValue) {
		return context_.getSharedPreferences(PREFERENCE_NAME,
				Context.MODE_PRIVATE).getInt(key, defValue);
	}

	public boolean putLong(String key, long value) {
		boolean flag = context_
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
				.edit().putLong(key, value).commit();
		return flag;
	}

	public long getLong(String key, long defValue) {
		return context_.getSharedPreferences(PREFERENCE_NAME,
				Context.MODE_PRIVATE).getLong(key, defValue);
	}

	public boolean putString(String key, String value) {
		boolean flag = context_
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
				.edit().putString(key, value).commit();
		return flag;
	}

	public String getString(String key, String defValue) {


		return context_.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getString(key, defValue);

	}
	public boolean putFloat(String key, Float value) {
		boolean flag = context_
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
				.edit().putFloat(key, value).commit();
		return flag;
	}

	public Float getFloat(String key, Float defValue) {


		return context_.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE).getFloat(key, defValue);

	}
	public void setFirstTime(boolean firsttime) {
		putBoolean("firsttime", firsttime);

	}

	public boolean getFirstTime() {

		return getBoolean("firsttime", true);

	}
	public void setFirstTimeAge(boolean firsttime) {
		putBoolean("firsttimeAge", firsttime);

	}

	public boolean getFirstTimeAge() {

		return getBoolean("firsttimeAge", true);

	}
	public void setFirstTimePermission(boolean firsttime) {
		putBoolean("firsttimePermission", firsttime);

	}

	public boolean getFirstTimePermission() {

		return getBoolean("firsttimePermission", true);

	}
	public void setSmsPermission(boolean smsPermission) {
		putBoolean("smsPermission", smsPermission);

	}

	public boolean getSmsPermission() {

		return getBoolean("smsPermission", false);

	}
	public void setPermission(boolean smsPermission) {
		putBoolean("permission", smsPermission);

	}

	public boolean getPermission() {

		return getBoolean("permission", false);

	}
	public void setProtcal(boolean firsttime) {
		putBoolean("protcal", firsttime);

	}

	public boolean getProtcal() {

		return getBoolean("protcal", true);

	}
	public boolean putBoolean(String key, boolean value) {
		boolean flag = context_
				.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)
				.edit().putBoolean(key, value).commit();
		return flag;
	}

	public boolean getBoolean(String key, boolean defValue) {
		// LogUtil.showPrint("canshu inner before::::");
		return context_.getSharedPreferences(PREFERENCE_NAME,
				Context.MODE_PRIVATE).getBoolean(key, defValue);

	}

	
	public boolean isShowMessage(){
		return  getBoolean("message", true);
	}


	/**
	 * 搜索历史清除
	 */
	public void clearList() {
		SharedPreferences mSharedPreference = context_.getSharedPreferences(PREFERENCE_NAME,Context.MODE_PRIVATE);
		SharedPreferences.Editor editor= mSharedPreference.edit();
		int size = mSharedPreference.getInt("historySize", 0);
		for (int i = 0; i < size; i++) {
			editor.remove("history_"+i);
			editor.commit();
		}
		editor.remove("historySize");
		editor.commit();

	}

	//清除本地用户相关信息
	public void clearAll(Context context) {
		clearList();
		clearUserInfo();
	}







	//清除本地用户相关信息
	public void clearUserInfo() {
		SharedPreferences preferences = context_.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = preferences.edit();
//		editor.remove("userNumber");
//		editor.remove("nickName");
//		editor.remove("easemobId");
//		editor.remove("easemobPwd");
//		editor.remove("userToken");
//		editor.remove("isLogin");
//		editor.remove("loginUserId");
//		editor.remove("userToken");
//		editor.remove("payPwd");
//		editor.remove("isIdCard");//实名认证
//		editor.remove("inviteUrl");//实名认证
		editor.commit();
	}
}
